<?php namespace App\Models\Power;

use App\Models\Base\PowerBase;
use App\Models\Power\Order;
use App\Models\Power\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Recharge extends PowerBase {

    protected $table = 'cdz_recharge';

    protected $fillable = [];

    protected $dates = [];

    public static $rules = [
        // Validation rules
    ];

    public $timestamps = false;

    /**
     * 根据uid查询充值记录
     * params1 string $uid
     * params2 Int $pile_id
     */
    public function getMoneyDetail($uid) {

        $query = self::query();
        $query -> where('user_uid', $uid);
        $query -> whereStatus(2);
        $recharge = $query -> get() -> toArray();

        $result = [];
        
        if($recharge) {
            foreach($recharge as $key => $value) {
                $temp['title'] = '充值入账';
                $temp['time'] = $value['created_at'];
                $temp['money'] = '+' . $value['money'];
                $temp['no'] = $value['no'];
                $temp['type'] = 1;
                $temp['way'] = $value['way'];
                $result[] = $temp;
            }
        }

        $charge = Order::whereStatus(5) 
                        -> where('user_uid', '=', $uid)
                        -> get() 
                        -> toArray();
        
        if($charge) {
            foreach($charge as $key => $value) {
                $temp['title'] = '充电消费';
                $temp['time'] = $value['end'];
                $temp['money'] = '-' . $value['money'];
                $temp['no'] = $value['no'];
                $temp['type'] = 2;
                $result[] = $temp;
            }
        }

        $result = multi_array_sort($result, 'time');

        foreach($result as $key => $value) {
            if($value['type'] == 2) {
                unset($result[$key]['way']);
            }
        }
        
        return $result;
        
    }

    public function getDetailInfo($no, $uid) {

        $data = [];
        $data = self::where('no', '=', $no) -> where('user_uid', $uid) -> first();

        if($data) {
            $data['type'] = 1;
        }
        
        return $data;

    }

    /**
     * 处理充电桩充值
     * @param string $no 回调返回的数据
     * @param int   $way 支付方式
     * @return bool
     */
    public function recharge($no, $way) {

        $query = self::query();
        $query -> where('no', $no);
        $query -> whereStatus(1);
        $recharge = $query -> first();
        if(!$recharge) {
            return true;
        }
        DB::connection('jmft_power') -> beginTransaction();
        $recharge -> way = $way;
        $recharge -> status = 2;
        $recharge -> pay_time = time();
        $recharge_save = $recharge -> save();
        if(!$recharge_save) {
            DB::connection('jmft_power') -> rollBack();
            return false;
        }

        $user_save = User::addMoney($recharge -> user_id, $recharge -> money);
        if(!$user_save) {
            DB::connection('jmft_power') -> rollBack();
            return false;
        } else {
            DB::connection('jmft_power') ->commit();
            return true;
        }


    }

    /**
     * 和Pile模型的多对一关联
     */
    // public function pile() {
    //     return $this->belongsTo('App\Models\Pile', 'pile_id');
    // }



}
